﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using TowerDefender.Entities;
using TowerDefender.Model;

namespace TowerDefender.Graphics.Animated.Mobs
{
    /// <summary>
    /// Interaction logic for Peon.xaml
    /// </summary>
    public partial class PeonGraphics : UserControl
    {
        private double _stepX;
        private double _stepY;
        private Engine _engine;
        public Peon peon { get; set; }
        public Coordinates OldPosition { get; set; }

        public double StepX 
        {
            get
            {
                return _stepX;
            }
        }
        public double StepY
        {
            get
            {
                return _stepY;
            }
        }
        public Engine engine
        {
            get { return _engine; }
        }

        public PeonGraphics(Peon p, double stepX, double stepY, Engine engine)
        {
            InitializeComponent();
            peon = p;
            OldPosition = p.coordinates;
            _stepX = stepX;
            _stepY = stepY;
            _engine = engine;
            _engine.Tick += new EventHandler(engine_Tick);
            PeonManager.DrawPeon(this, peon);
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
            peon.PositionInMapPath++;
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            
            if (!this.Completed)
            {
                if (peon.PositionInMapPath < peon.PeonMap.MapPath.Steps.Count - 1)
                {
                    PeonManager.MovePeon(this);
                    OldPosition = peon.coordinates;
                }

                else
                {
                    this.Visibility = System.Windows.Visibility.Hidden;
                }
            }
        }

        

        public void da_Completed(object sender, EventArgs e)
        {
            
            this.SetValue(Grid.ColumnProperty, peon.coordinates.X - 1);
            this.SetValue(Grid.RowProperty, peon.coordinates.Y - 1);
            peon.PositionInMapPath++;
        }

        public bool Completed
        {
            get
            {
                return (this.OldPosition == peon.coordinates);
            }
        }
        void engine_Tick(object sender, EventArgs e)
        {
            if (_engine.TickCount % peon._speed == 0)
            {
                //peon.PositionInMapPath++;
                //this.SetValue(Grid.ColumnProperty, peon.coordinates.X - 1);
                //this.SetValue(Grid.RowProperty, peon.coordinates.Y - 1);
            
            }
        }
        
    }
}
